package com.itheima.service.impl;

import com.aliyun.oss.common.utils.DateUtil;
import com.itheima.common.utils.DateUtils;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;



    /**
     * 根据日期修改可预约人数
     */
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        Long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0){
            orderSettingMapper.updataNumberByOrderDate(orderSetting);
        }
        orderSettingMapper.addNumberByOrderDate(orderSetting);
    }


    /**
     * 批量导入预约设置数据
     */
    public void addNumberByOrderDate(List<OrderSetting> data) {
        if (data != null && data.size() > 0){
            for (OrderSetting orderSetting : data) {
                //判断日期是否已经进行预约
                Long orderSettingByMonth = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if (orderSettingByMonth >0){
                    orderSettingMapper.updataNumberByOrderDate(orderSetting);
                }
                orderSettingMapper.addNumberByOrderDate(orderSetting);
            }
        }
    }

    /**
     * 根据年月查询预约设置数据
     */
    public  List<Map> findOrderSettingByMonth(String month) throws Exception {
        String begin = month + "-1";
        String end = month + "-31";
            Date beginDate = DateUtils.parseString2Date(begin);
            Date endDate = DateUtils.parseString2Date(end);
        List<OrderSetting> list =  orderSettingMapper.getOrderSettingByMonth(beginDate,endDate);
        List<Map> result = new ArrayList<>();
        if (list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> map = new HashMap();
                map.put("date",orderSetting.getOrderDate().getDate());
                map.put("number",orderSetting.getNumber());
                map.put("reservations",orderSetting.getReservations());
                result.add(map);
            }
        }
        return result;
//     Date mouth = DateUtils.parseString2Date(month);
//     return orderSettingMapper.getOrderSettingByMonth(mouth);
    }



}
